package com.senyint.sys.utils.pool;

import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public abstract class Pool<T> {
	private final GenericObjectPool<T> internalPool;

	public Pool(PooledObjectFactory<T> factory, GenericObjectPoolConfig poolConfig) {
		this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
	}

	public T getResource() {
		try {
			return this.internalPool.borrowObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void returnResource(T resource) {
		try {
			this.internalPool.returnObject(resource);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void destroy() {
		try {
			this.internalPool.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int inPoolSize() {
		try {
			return this.internalPool.getNumIdle();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int borrowSize() {
		try {
			return this.internalPool.getNumActive();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}